﻿using QLXN.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QLXN.Product
{
    public partial class ProductList : hTekForm
    {
        private const int CP_NOCLOSE_BUTTON = 0x200;
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ClassStyle = cp.ClassStyle | CP_NOCLOSE_BUTTON;
                return cp;
            }
        }

        public ProductList()
        {
            InitializeComponent();
        }

        public ProductList(string username)
        {
            InitializeComponent();
            txt_us.Text = username;
            context.Font = this.Font;
        }

        SqlInteraction sql = new SqlInteraction();
        
        private void ProductList_Load(object sender, EventArgs e)
        {
            load_product_list();
        }

        private void load_product_list()
        {
            try
            {
                string query = "SELECT qlxn_products.id, qlxn_products.name, qlxn_manufacturers.name AS manufacturer, qlxn_products.unit, qlxn_products.detail, ISNULL(qlxn_products.stored, 0) AS stored, qlxn_products.last_modify, qlxn_products.last_modify_time FROM qlxn_products INNER JOIN qlxn_manufacturers ON qlxn_products.manufacturer = qlxn_manufacturers.id";
                DataTable dt = new DataTable();
                dt = sql.Execute_Table(query);
                gvProduct.DataSource = dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region gridview function
        private void gvProduct_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0 && e.Button == MouseButtons.Right)
            {
                gvProduct.Rows[e.RowIndex].Selected = true;
                Rectangle r = gvProduct.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
                context.Show((Control)sender, r.Left + e.X, r.Top + e.Y);
            }
            load_modifier();
        }

        private void gvProduct_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                context.Show(gvProduct, e.X, e.Y);
            }
        }

        private void gvProduct_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            load_modifier();
        }

        private void load_modifier()
        {
            if (gvProduct.SelectedRows.Count > 0)
            {
                txt_last_modify.Text = gvProduct.SelectedRows[0].Cells[7].Value.ToString();
                string t = gvProduct.SelectedRows[0].Cells[8].Value.ToString();
                if (t != "")
                {
                    DateTime time = Convert.ToDateTime(t);
                    txt_last_modify_time.Text = time.ToString("HH:mm dd/MM/yyyy");
                }
            }
        }
        #endregion
        
        private void menu_add_Click(object sender, EventArgs e)
        {
            ProductEdit pe = new ProductEdit(txt_us.Text);
            if (pe.ShowDialog() == DialogResult.OK)
                load_product_list();
        }

        private void menu_edit_Click(object sender, EventArgs e)
        {
            if (gvProduct.SelectedRows.Count > 0)
            {
                string id = gvProduct.SelectedRows[0].Cells[1].Value.ToString();
                ProductEdit pe = new ProductEdit(id, txt_us.Text);
                if (pe.ShowDialog() == DialogResult.OK)
                    load_product_list();
            }
            else
                MessageBox.Show(this, "Chưa chọn dữ liệu!" + Environment.NewLine, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop);
        }

        private void menu_delete_Click(object sender, EventArgs e)
        {
            if (gvProduct.SelectedRows.Count > 0)
            {
                string id = gvProduct.SelectedRows[0].Cells[1].Value.ToString();
                DialogResult rs = MessageBox.Show(this, "Xóa dữ liệu đang chọn!" + Environment.NewLine, "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (rs == DialogResult.Yes)
                {
                    string query = "DELETE FROM qlxn_products WHERE id = '" + id + "'";
                    bool flag = sql.Execute_Sql(query);
                    if (flag)
                    {
                        load_product_list();
                        MessageBox.Show(this, "Đã xóa dữ liệu!" + Environment.NewLine, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);                    }
                }
            }
            else
                MessageBox.Show(this, "Chưa chọn dữ liệu!" + Environment.NewLine, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop);
        }

        private void btn_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}